/**
 * 
 */
package com.armail.client.controller.impl;

import java.util.Collection;
import java.util.LinkedHashSet;

import javax.ws.rs.core.MediaType;

import com.armail.client.controller.RegistrationController;
import com.armail.client.controller.RegistrationControllerListener;
import com.armail.client.dto.User;
import com.armail.client.view.RegistrationView;
import com.armail.client.view.RegistrationViewListener;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

/**
 * @author arpit
 * 
 */
public class RegistrationControllerImpl extends ControllerImpl implements
		RegistrationController, RegistrationViewListener {

	private final RegistrationView registrationView;
	private User model;
	private final Collection<RegistrationControllerListener> listeners = new LinkedHashSet<>();

	@Inject
	public RegistrationControllerImpl(Injector injector,
			RegistrationView registrationView) {
		super(injector);
		this.registrationView = registrationView;
		this.registrationView.addListener(this);
	}

	private static final String REST_URI = "http://localhost:8080/com-armail-server/";
	private static final String CREATE = "/users/create";

	public User createUser(User user) {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource resource = client.resource(REST_URI);

		WebResource employeeResource = resource.path("/rest/").path(CREATE);
		final ClientResponse response = employeeResource.accept(
				MediaType.APPLICATION_XML).post(ClientResponse.class, user);
		User createdUser = response.getEntity(User.class);
		return createdUser;
	}

	@Override
	public void activate() {
		registrationView.setModel(getModel());
		registrationView.attach();
	}

	private User getModel() {
		if (this.model == null) {
			model = new User();
		}
		return this.model;
	}

	@Override
	public void deactivate() {
		registrationView.detach();
	}

	@Override
	public void onSave() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource resource = client.resource(REST_URI);

		WebResource employeeResource = resource.path("/rest/").path(CREATE);
		final ClientResponse response = employeeResource.accept(
				MediaType.APPLICATION_XML).post(ClientResponse.class, model);
		User createdUser = response.getEntity(User.class);
		if(createdUser != null) {
			for (RegistrationControllerListener l : listeners) {
				l.onRegister();
			}
		}
	}

	@Override
	public void addListener(
			RegistrationControllerListener registrationControllerListener) {
		this.listeners.add(registrationControllerListener);
	}

	@Override
	public void removeListener(
			RegistrationControllerListener registrationControllerListener) {
		this.listeners.remove(registrationControllerListener);
	}

	@Override
	public void onCancel() {
		for (RegistrationControllerListener l : listeners) {
			l.onCancel();
		}
	}
}
